//
//  LeftViewController.m
//  aMoment
//
//  Created by dllo on 15/9/30.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "LeftViewController.h"
#import "AllHeaderFiles.h"

@interface LeftViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)UIViewController *rootVC;
@property(nonatomic, retain)MusicView *musicView;//音乐播放条
@property(nonatomic, retain)NSArray *itemNameArray;//菜单项目名称
@property(nonatomic, retain)NSArray *itemImageArray;//菜单项目图片
@property(nonatomic, retain)UIImageView *downloadImageView;
@property(nonatomic, retain)UIImageView *collectImageView;
@property(nonatomic, retain)UIImageView *searchImageView;
@end

@implementation LeftViewController

- (void)dealloc
{
    [_tableView release];
    [_rootVC release];
    [_musicView release];
    [_itemNameArray release];
    [_itemImageArray release];
    [_downloadImageView release];
    [_collectImageView release];
    [_searchImageView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.itemNameArray = [NSArray arrayWithObjects:@"首页", @"电台", @"阅读", @"社区", @"碎片", @"良品", @"设置", nil];//菜单项目名称
    self.itemImageArray = [NSArray arrayWithObjects:@"iconfont-home@2x", @"iconfont-radio@2x", @"iconfont-book@2x", @"iconfont-community@2x", @"iconfont-timeline@2x", @"iconfont-shop@2x", @"iconfont-set@2x", nil];//菜单项目图片
}

- (void)loadView
{
    [super loadView];
    //背景图片
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
    backgroundImageView.clipsToBounds = YES;
    backgroundImageView.image = [UIImage imageNamed:@"menu-background"];
    [self.view addSubview:backgroundImageView];
    [backgroundImageView release];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 50 * FIT_HEIGHT, SCREEN_WIDTH, 44 * 7) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = NO;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    //创建音乐播放条
    self.musicView = [[MusicView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 80 * FIT_HEIGHT, SCREEN_WIDTH, 80 * FIT_HEIGHT)];
    _musicView.backgroundColor = [UIColor blackColor];
    _musicView.alpha = .8;
    [self.view addSubview:_musicView];
    [_musicView release];
    
    //下载, 收藏, 搜索
    NSArray *imageArray = @[@"iconfont-search@2x", @"iconfont-star@2x", @"iconfont-download@2x"];
    CGFloat imageViewY = SCREEN_HEIGHT - _musicView.Height - 50 * FIT_HEIGHT - IMAGE_32;
    if ([UIScreen mainScreen].bounds.size.height < 500) {
        //适配iPhone4和4s
        imageViewY = 64.0 - 16.0 * FIT_WIDTH;
    }
    for (NSInteger i = 0; i < 3; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((250 - 32 - (20 + 32) * i) * FIT_WIDTH, imageViewY, IMAGE_32, IMAGE_32)];
        imageView.tag = 1000 + i;
        imageView.image = [UIImage imageNamed:imageArray[i]];
        imageView.userInteractionEnabled = YES;
        [self.view addSubview:imageView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageViewAction:)];
        [imageView addGestureRecognizer:tap];
        [tap release];
        [imageView release];
        if (i == 2) {
            _downloadImageView = imageView;//下载
        } else if (i == 1) {
            _collectImageView = imageView;//收藏
        } else {
            _searchImageView = imageView;//搜索
        }
    }
    [_downloadImageView removeFromSuperview];
}

#pragma mark 点击下载, 收藏, 搜索方法
- (void)clickImageViewAction:(UITapGestureRecognizer *)tap
{
    UIViewController *vc = [[UIViewController alloc] init];
    if (tap.view.tag == 1000) {
        vc = [[SearchViewController alloc] init];//搜索
    }
    else if (tap.view.tag == 1001) {
        vc = [[CollectViewController alloc] init];//收藏
    }
    else {
        vc = [[DownloadViewController alloc] init];//下载
    }
    [(UINavigationController *)self.view.window.rootViewController pushViewController:vc animated:NO];
    [(DDMenuController *)self.view.window.rootViewController showRootController:YES];//显示主界面
    [vc release];
}

#pragma mark cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _itemNameArray.count;
}

#pragma mark cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.imageView.image = [UIImage imageNamed:_itemImageArray[indexPath.row]];
    cell.textLabel.text = _itemNameArray[indexPath.row];
    return cell;
}

#pragma mark cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //找到根视图
    DDMenuController *menuController = (DDMenuController *)[UIApplication sharedApplication].delegate.window.rootViewController;
    
    switch (indexPath.row) {
        case 0://首页
            _rootVC = [[ViewController alloc] init];
            break;
        case 1://电台
            _rootVC = [[RadioViewController alloc] init];
            break;
        case 2://阅读
            _rootVC = [[ReadViewController alloc] init];
            break;
        case 3://社区
            _rootVC = [[CommunityViewController alloc] init];
            break;
        case 4://碎片
            _rootVC = [[TimelineViewController alloc] init];
            break;
        case 5://良品
            _rootVC = [[StoreViewController alloc] init];
            break;
        case 6://设置
            _rootVC = [[SetViewController alloc] init];
            break;
            
        default:
            break;
    }
    UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:_rootVC];
    
    //显示主界面
    [menuController setRootController:rootNav animated:YES];
    [_rootVC release];
//    [rootNav release];//不能release
}

#pragma mark 视图即将出现
- (void)viewWillAppear:(BOOL)animated
{
    Music *music = [MusicSingleton shareInstance].musicModel;
    
    //改变播放按钮图片
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"rate"]) {
        _musicView.playImageView.image = [UIImage imageNamed:@"iconfont-menustop"];
    } else {
        _musicView.playImageView.image = [UIImage imageNamed:@"iconfont-menuplay"];
    }
    
    //改变正在播放详情
    _musicView.musicURL = music.musicURL;
    [_musicView.coverImageView setImageWithURL:[NSURL URLWithString:music.coverImageURL] placeholderImage:[UIImage imageNamed:@"placeholder-cd"]];
    _musicView.songNameLabel.text = music.songName;
    _musicView.singerLabel.text = music.singer;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
